<?php
require_once "json.php";
require_once "xml.php";
require_once "session.php";
class NewBudget_Quota{
	private $quotaFile="../db/petro_06.xml";
	private $xslFile="../db/json.xsl";
	private $cacheFile="../db/quotajson.js";
	public function __construct(){
		$this->quotaDb=new NewBudget_XmlDb($this->quotaFile);
	}
	private function transformXml($xmlFile,$xslFile){
		$proc=new XsltProcessor();
		$proc->importStylesheet(DOMDocument::load($xslFile));
		return $proc->transformToXML(DOMDocument::load($xmlFile));
	}
	public function makeJsonString(){
		file_put_contents($this->cacheFile,$this->makeJsStr());
	}
	public function makeQuotaHtml(){
		echo $this->transformXml($this->quotaFile,"../db/html.xsl");
	}
	public function makeJsStr(){
		$doc=$this->xmldb=new NewBudget_XmlDb("../db/petro_06.xml");
		$str="var QUOTATREE={'T6':{'index':'".$doc->xselect('/volume')->item(0)->getAttribute("index")."','name':'".$doc->xselect('/volume')->item(0)->getAttribute("name")."'},'T6-2':{'index':'".$doc->xselect("/volume/chapter")->item(0)->getAttribute("index")."','name':'".$doc->xselect("/volume/chapter")->item(0)->getAttribute("name")."'},'T6-2-1':{'index':'".$doc->xselect("/volume/chapter/section")->item(0)->getAttribute("index")."','name':'".$doc->xselect("/volume/chapter/section")->item(0)->getAttribute("name")."'}";
		$work=$doc->xselect("/volume/chapter/section/work");
		for($i=0;$i<$work->length;$i++){
			$str.=",'T6-2-1-".$work->item($i)->getAttribute("index")."':{'index':'".$work->item($i)->getAttribute("index")."','name':'".$work->item($i)->getAttribute("name")."','content':'".$work->item($i)->getAttribute("content")."','unit':'".$work->item($i)->getAttribute("unit")."',item:[";
			$item=$doc->xselect("/volume/chapter/section/work[@index='".$work->item($i)->getAttribute("index")."']/item");
			for($j=0;$j<$item->length;$j++){
				if($j>0)	$str.=",";
				$str.="{'index':'".$item->item($j)->getAttribute("index")."','name':'".$item->item($j)->getAttribute("name")."','workinghour':'".$item->item($j)->firstChild->firstChild->data."'}";
			}
			$str.="]}";
		}
		$str.="}";
		return $str;
	}
	private function makeXps($budgetNo){
		$result="";
		foreach(split("-",$budgetNo) as $rank)		$result .= "/*[@index=$rank]";
		return $result;
	}
	public function modifybudget($args){
		$budgetNo=$args["budgetno"];
		$data=$args["data"];
		$node=$this->quotaDb->xselect($this->makeXps($budgetNo))->item(0);
		foreach($data as $key=>$value){
			if("workinghour"==$key){
				$node->firstChild->firstChild->data=$value;
				continue;
			}
			$node->setAttribute($key,$value);
		}
		$this->quotaDb->saveDoc();
		$this->makeJsonString();
		return Array("result"=>1);
	}
}
$quota=new NewBudget_Quota();
switch($_GET["action"]){
	case "show":
		$quota->makeQuotaHtml();
		break;
	case "js":
		//$quota->makeJsonString();
		//echo $quota->makeJsStr();
		break;
	case "modifybudget":
		echo NewBudget_Json::encode($quota->modifybudget(NewBudget_Json::decode(stripslashes($_POST["json"]))));
		break;
}
?>